package nancal.mp.db.es.entity

import nbcp.base.data.HttpRequestData
import nbcp.base.data.HttpResponseData
import nbcp.base.db.annotation.Cn
import nbcp.base.db.annotation.DbEntityGroup
import nbcp.base.db.annotation.DbName
import nbcp.base.extend.minus
import nbcp.base.extend.totalSeconds
import nbcp.base.model.BaseIdEntity
import nbcp.myoql.db.comm.DbDefine
import nbcp.myoql.db.comm.IkFieldDefine
import nbcp.myoql.db.es.base.IEsDocument
import java.time.LocalDateTime


@DbName("file-log")
@IkFieldDefine("path", "query", "requestBody", "responseBody", "msg")
@DbDefine(
    "path", """
{
    "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
    }
}
"""
)
@DbEntityGroup("system")
open class FileLog : BaseIdEntity(), IEsDocument {
    var serviceName = ""
    var traceId: String? = null
    var requestId: String? = null
    var logLevel: String? = ""

    var isRequest: Boolean? = false
    var path = ""
    var query = ""
    var ip = ""
    var recvAt: LocalDateTime? = null

    var appName: String? = null

    var execMilliSeconds = 0
    var triggerAt: LocalDateTime? = null

    var adjustId: String? = null
    var pkgName: String? = null
    var deviceId = ""
    var userId = ""
    var requestBody = ""
    var status = 0
    var responseBody = ""

    var remark: String? = ""
    var sort: Long? = 0
    var createAt = LocalDateTime.now()
    var msg: String? = ""

    @Cn("网络请求时间")
    var netMilliSeconds: Int = 0

    @Cn("总时长")
    var totalMilliSeconds: Int = 0
    fun compute() {
        if (recvAt == null) {
            netMilliSeconds = 0;
        } else if (triggerAt == null) {
            netMilliSeconds = 0
        } else {
            netMilliSeconds = (recvAt!!.totalSeconds - triggerAt!!.totalSeconds).toInt() * 1000
        }


        totalMilliSeconds = netMilliSeconds + execMilliSeconds
    }

}